/**
 * Copyright (C) 2008 aileron.cc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package cc.aileron.plugin.popup.actions;

import java.io.File;
import java.io.IOException;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IActionDelegate;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbenchPart;

/**
 * create sql file
 * 
 * @author Aileron
 * 
 */
public class CreateSqlFileAction implements IObjectActionDelegate
{
    /**
     * shell
     */
    private Shell shell;

    /**
     * Constructor for Action1.
     */
    public CreateSqlFileAction()
    {
        super();
    }

    /**
     * @see IObjectActionDelegate#setActivePart(IAction, IWorkbenchPart)
     */
    public void setActivePart(
            final IAction action,
            final IWorkbenchPart targetPart)
    {
        shell = targetPart.getSite().getShell();
    }

    /**
     * @see IActionDelegate#run(IAction)
     */
    public void run(IAction action)
    {
        final Object object = selection.getFirstElement();
        final ICompilationUnit unit = (ICompilationUnit) object;
        
        try
        {
            createSqlFile(unit.findPrimaryType());
        }
        catch (Throwable e)
        {
        }
        MessageDialog.openInformation(shell, "cc.aileron.plugin CreateSqlFile",
               "[" + unit.findPrimaryType().getElementName() + "] as create sql file");
    }
    
    /**
     * 型を読みこみ
     * @param type
     * @throws JavaModelException
     * @throws IOException 
     */
    private void createSqlFile(final IType type) throws JavaModelException, IOException
    {
        final String rootName = type.getElementName();
        final File root = type.getResource().getLocation().toFile().getParentFile();
        for(IMethod method : type.getMethods())
        {
            final String methodName = method.getElementName() ;
            final File sqlFile = new File(root,rootName+"_"+methodName+".sql");
            if(!sqlFile.exists())
            {
                sqlFile.createNewFile();
            }
        }
    }

    /**
     * @see IActionDelegate#selectionChanged(IAction, ISelection)
     */
    public void selectionChanged(
            final IAction action,
            final ISelection iselection)
    { 
        this.selection = (StructuredSelection) iselection;
    }
    
    /**
     * selection
     */
    private StructuredSelection selection;
}